<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
class Document extends Mycontroller
{
    public function __construct()
    {
        Parent::__construct();
        // $classList = db('class')->select();
        // $this->assign('classList',$classList);
        $classlist =db('class')->select();
        $this->assign('classlist',$classlist);
        $taglist =db('tag')->select();
        $this->assign('taglist',$taglist);

    }

//审核失败
    public function nopathinfo()
    {
        $_POST['status'] = ($_POST['status']==0?2:5);
        $result = db('ask')->update($_POST);
        if($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
//审核成功
    public function eheckpass($id,$status){

        $status = $status == 0?1:4;
        $data = [
            'id'=>$id,
            'status'=>$status
        ];
        $result = db('ask')->update($data);
        if($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }

//文件归档
    public function classify(){
        $result = db('ask')->update($_POST);
        if($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }

//催文
    public function addurge(){
        //找到该id对应的文书名称, 部门名称 ->拼接标题信息
        $user = db('user')->where('id','=',Session::get('userid'))->find();
        $classname = db('class')->where('id','=',$user['class'])->value('name');
        //给对方局长id
        $getid = db('user')->where('class',$_POST['outcid'])->where('position','局长')->value('id');
        //拼接信息 
        $data = [
            'newsname' =>'来自:'.$classname.'('.$_POST['textname'].')的催文',
            'content' => $_POST['urgetext'],
            'sendid' =>0,
            'getid' => $getid,
            'sendtime' => time(),
            'status' =>0
        ];
        $result = db('news')->insert($data);
        if($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
//查看文书列表
    public function bookslist()
    {
        //查询职位 判定是否有资格查看加密文件
        $userinfo = db('user')->where('id','=',Session::get('userid'))->find();
        $pos = $userinfo['position'];
        $class = $userinfo['class'];

        if($pos == '普通职员'){
            $isencrypt = "isencrypt = 0";
        }else{
            $isencrypt = true;
        }
        //发送部门名称列表至前台
        $classnamelist = db('class')->select();
        $this->assign('classnamelist',$classnamelist);
        //发送文件类型列表到前台
        $taglist = db('tag')->select();
        $this->assign('taglist',$taglist);
        //查询文件 
        $textlist = db('ask')->where($isencrypt)->where('askcid','=',$class)->where('status','eq',6)->select();
        $this->assign('textlist',$textlist);
        return  $this->fetch('bookslist');
        // var_dump($list);die;
    }

 public function gopushdocument()
    {
        return $this->fetch('pushdocument');
    }
//起草新文书
    public function gonewdocument()
    {
        //查询部门类表发送前台
        $classlist =db('class')->select();
        $this->assign('classlist',$classlist);
        return $this->fetch('newdocument');
    }
    public function newdocument()
    {

        if(empty($_POST['name'])||empty($_POST['editorValue'])){
             echo  "<script>alert('数据不能为空!');history.back()</script>";
            return ;
        }
        $data=[
                    'askuid'=>$_POST['id'],
                    'asktime'=>time(),
                    'askcid'=>$_POST['classid'],
                    'textname'=>$_POST['name'],
                    'content'=>$_POST['editorValue'],
                ];


        switch ($_POST['type']){
            case 1:
                $data['type']=0;
                break;
            case 2:
                $data['type']=1;
                break;
            case 3:
                $data['type']=2;
                $data['outcid']=$_POST['outcid'];
                break;
        }
        $res = db('ask')->insert($data);
        if($res){
            return $this->success('操作成功','UserIndex/index');
        }else{
            echo  "<script>alert('操作失败!');history.back()</script>";
            return ;
        }
    }

    public function asklist(){
        //查询职位 判定是否有资格查看加密文件
        $userinfo = db('user')->where('id','=',Session::get('userid'))->find();
        $pos = $userinfo['position'];
        $class = $userinfo['class'];

        if($pos == '普通职员'){
            $isencrypt = "isencrypt = 0";
        }else{
            $isencrypt = true;
        }
        //查询文件 
        $list1 = db('ask')->where($isencrypt)->where('askcid','=',$class)->where('status','neq',6)->select();
        //查询外部传输需本部审核的文件 (需原部门审核完成)
        $list2 = db('ask')->where($isencrypt)->where('outcid','=',$class)->where('status','=','1')->select();
         $textlist = array_merge($list1,$list2);
         //查询所有部门名称发送至前台
        $classnamelist = db('class')->select();
        $this->assign('classnamelist',$classnamelist);
        if(empty($textlist)){
            $this->assign('textlist','');
        }else{
           $this->assign('textlist',$textlist);
        }
        // var_dump($textlist);die;
        return $this->fetch('Document/asklist');
    }

//查看文书
    public function watchtext($id)
    {
        //查询本文件信息
        $textinfo = db('ask')->where('id','=',$id)->find();
        $this->assign('textinfo',$textinfo);

        //发送 申请人章,申请部门,审核部门 印章路径
        $personalurl = db('user')->where('id','=',$textinfo['askuid'])->value('picurl');
        $innercidurl = db('class')->where('id',$textinfo['askcid'])->value('printurl');
        $outcidurl = db('class')->where('id',$textinfo['outcid'])->value('printurl');
        //判定发送选项
        
        switch ($textinfo['type']){
            case 0:
                $this->assign('img1',$personalurl);
                $this->assign('img2',$innercidurl);
                break;
            case 1:
                $this->assign('img1','');
                $this->assign('img2',$innercidurl);
                break;
            default:
                $this->assign('img1',$innercidurl);
                $this->assign('img2',$outcidurl);
                break;

        }
        // var_dump($personalurl,$innercidurl,$outcidurl);die;
        return $this->fetch('watchtext');
    }

    public function pushdocument()
    {
        // var_dump(input(''));die;

        if(empty($_POST['name'])){
             echo  "<script>alert('文件名不能为空!');history.back()</script>";
            return ;
        }
        //判断是否有上传文件
        if($_FILES['content']['error']===0){
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('content');
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'Mydocuments');
            if($info){
                // 成功上传后 获取上传
                $path = $info->getSaveName();
                //转换路径
                $path = str_replace('\\', '/', $path);
                if($path){
                    //读取文件内容存到字符串中
                    $content = file_get_contents("__STATIC__/../Mydocuments/$path");
                    return $this->success('上传成功','UserIndex/index');
                }
            }
        }
        // 上传失败
        echo  "<script>alert('上传文件失败!');history.back()</script>";
        return ;
    }



    public function pass($id)
    {
        //将审核人的id 审核时间 状态进行更改
        $data = [
            'id'=>$id,
            'inneruid'=>Session::get('userid'),
            'innertime'=>time(),
            'status'=>1
        ];
        $res = db('ask')->update($data);
        if($res){
            return $this->success('审核成功','Document/asklist');
        }else{
            echo  "<script>alert('审核失败!');history.back()</script>";
            return ;
        }
        var_dump($id);die;
    }
    public function reject()
    {
        if(empty($_POST['question'])){
            echo  "<script>alert('请输入否决原因!');history.back()</script>";
            return ;
        }
        $data = [
            'id'=>$_POST['id'],
            'inneruid'=>Session::get('userid'),
            'innertime'=>time(),
            'status'=>2,
            'question'=>$_POST['question']
        ];
         $res = db('ask')->update($data);
        if($res){
            return $this->success('操作成功','Document/asklist');
        }else{
            echo  "<script>alert('操作成功!');history.back()</script>";
            return ;
        }
    }
}